package cn.b.sky.mes.hr.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.hr.model.PersonnelType;
import cn.b.sky.mes.hr.model.condition.PersonnelTypeCondition;
import cn.b.sky.mes.hr.service.PersonnelTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/personnelType/*")
@Slf4j
public class PersonnelTypeController extends SecurityController {

  private static final String PERSONNELTYPE_LIST_PAGE = "hr/personnelType/personnelType_list";

  // private static final String PERSONNELTYPE_ADD_PAGE =
  // "hr/personnelType/personnelType";

  @Autowired
  private PersonnelTypeService personnelTypeService;

  @RequestMapping("personnelType_list")
  public String personnelTypeListPage(String navTabId, PersonnelTypeCondition condition, Model model) {
    model.addAttribute(NAVTABID, navTabId);

    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }

    List<PersonnelType> list = personnelTypeService.getPersonnelTypeListByCondition(condition);
    int count = personnelTypeService.getPersonnelTypeCountByCondition(condition);

    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return PERSONNELTYPE_LIST_PAGE;
  }

  @RequestMapping("personnelType_add")
  public String addPersonnelType(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "hr/personnelType/personnelType_add";
  }

  @RequestMapping("viewPersonnelType")
  public String viewPersonnelType(Model model, String navTabId, String id) {
    PersonnelType personnelType = personnelTypeService.getPersonnelTypeInfo(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("personnelType", personnelType);
    return "hr/personnelType/personnelType_view";
  }

  @RequestMapping("savePersonnelType")
  @ResponseBody
  public DWZResult savePersonnelType(Model model, String navTabId, String id, PersonnelType personnelType,
      HttpServletRequest request) {
    if (personnelType == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          SystemConstant.PARAMETERNULL));
    }
    try {
      this.setCreateInfo(personnelType);
      personnelType.setSiteCode(SecurityContextUtil.getSiteCode());

      if (personnelType.getLockFlag() == null) {
        personnelType.setLockFlag(0);
      }
      if (StringUtils.isEmpty(id)) {
        personnelTypeService.insertPersonnelType(personnelType);
        String forwardUrl = getForwardUrl(request) + "/personnelType/personnelType_list?navTabId=" + navTabId;
        return dialogAjaxDone(
            getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "successfulOperation.M"),
            navTabId, null, forwardUrl, null);
      } else {
        personnelTypeService.updatePersonnelType(personnelType);
        return dialogAjaxDone(
            getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "successfulOperation.M"),
            null, null, null, null);
      }
    } catch (Exception e) {
      log.error("{}", e);
      model.addAttribute("message", e.getMessage());
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "failedOperation.M"));
    }
  }

  @RequestMapping("modifyPersonnelType")
  public String modifyPersonnelType(Model model, String navTabId, String id) {
    PersonnelType personnelType = personnelTypeService.getPersonnelTypeInfo(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("personnelType", personnelType);
    return "hr/personnelType/personnelType_edit";
  }

  @RequestMapping("validatePersonnelTypeCode")
  @ResponseBody
  public boolean validatePersonnelTypeCode(String personnelTypeCode, Model model) {
    if (!StringUtils.isBlank(personnelTypeCode)) {
      PersonnelType personnelType = personnelTypeService.getPersonnelTypeInfo(SecurityContextUtil.getSiteCode(),
          personnelTypeCode);
      return personnelType == null;
    }
    return false;
  }

  @RequestMapping("deletePersonnelType")
  @ResponseBody
  public DWZResult deletePersonnelType(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          SystemConstant.PARAMETERNULL));
    }

    String[] typeCodes = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(typeCodes.length);
    boolean hasFail = false;
    for (String typeCode : typeCodes) {
      BatchJobReport report = personnelTypeService.deletePersonnelType(this.getSiteCode(), typeCode);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping("personnelTypeSuggest")
  @ResponseBody
  public List<PersonnelType> personnelTypeSuggest(PersonnelTypeCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    return personnelTypeService.getPersonnelTypeSuggestByCondition(condition);
  }

  @RequestMapping("personnelTypeLookUp")
  public String personnelTypeLookUp(Model model, String navTabId,
      @ModelAttribute("condition") PersonnelTypeCondition condition) {
    //查询没锁的
    condition.setLockFlag(0);
    List<PersonnelType> list = personnelTypeService.getPersonnelTypeListByCondition(condition);
    int count = personnelTypeService.getPersonnelTypeCountByCondition(condition);

    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "hr/personnelType/personnelType_lookup_radio";
  }

  @RequestMapping("validatePersonnelType")
  @ResponseBody
  public boolean validatePersonnelType(String personnelTypeCode, Model model) {
    if (!StringUtils.isBlank(personnelTypeCode)) {
      PersonnelType personnelType = personnelTypeService.getPersonnelTypeInfo(SecurityContextUtil.getSiteCode(),
          personnelTypeCode);
      return personnelType != null;
    }
    return false;

  }

}
